#ifndef BASELIB_CQUEUE_H_
#define BASELIB_CQUEUE_H_

#include <iostream>
#include "typedef.h"
#include "object_auto_mgr.h"
#include "cmutex.h"

template<typename T>
class CQueue {
public:
    CQueue() : m_front(NULL), m_tail(NULL) {
        m_mutex.InitMutex();
    }
    ~CQueue() {}

    bool        GetFront(T** t);
    bool        EnQueue(const T*& t);
    uint32_t    GetCount() const;

private:

    T*                   m_front;
    T*                   m_tail;
    CMutex               m_mutex;
    uint32_t volatile    m_count;
};

template<typename T>
bool CQueue<T>::GetFront(T** t) {
    CMutexLockMgr auto_lock(m_mutex);
    if (m_count == 0) {
        *t = NULL;
        return false;
    }
    *t = m_front;
    m_front = m_front->next;
    --m_count;
    return true;
}

#endif//BASELIB_CQUEUE_H_